/*
Copyright (c) 2009, Young Digital Planet SA
All rights reserved.

* Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this 
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, 
  this list of conditions and the following disclaimer in the documentation 
  and/or other materials provided with the distribution.
* Neither the name of the Young Digital Planet SA nor the names of its 
  contributors may be used to endorse or promote products derived from this 
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
package com.yauthor.module
{
	import flash.utils.IDataInput;
	import flash.utils.IDataOutput;
		
	/**
	 * Contains information about the activity check operation.
	 * @see pl.ydp.p2.IActivity#check()
	 * @see pl.ydp.p2.ResultUtils#result()
	 */	
	public class Result implements IStatefull
	{
		private var m_evaluationPending:Boolean = false;
		private var m_todo:uint;
		private var m_done:uint;
		/**
		 * Errors that modify the result.
		 */
		private var m_errors:uint;
		/**
		 * Errors that do not modify the result
		 */
		private var m_errorsUncounted:uint;
		private var m_includeErrors:Boolean;
		
		/**
		 * Creates information about the activity result.
		 * @param _todo a number of elements that user should do
		 * @param _done a number of elements done correctly
		 * @param _errors a number of elements done wrongly
		 * @param _includeErrorsInResult should errors take part in the result 
		 * calculation. If true, errors are substracted from done to calculate
		 * the result, if false, the result will be proportional to done. This property
		 * should be taken from the configuration (style sheet) by an activity
		 * @param _evalPending the evaluation is pending, the result is not complete - needs
		 * additional data (from a student/teacher)
		 */ 
		public function Result( _todo:uint = 0, _done:uint = 0, _errors:uint = 0 , _includeErrorsInResult:Boolean = true, _evalPending:Boolean = false ) {
			init( _todo, _done, _errors, _includeErrorsInResult, _evalPending );
		}
		
		/**
		 * Activity awaits evaluation.
		 */
		public function get evaluationPending():Boolean {
			return m_evaluationPending;
		}
		
		/**
		 * How many elements user should do. <br> 
		 * WARNING: done + errors can be different than todo
		 */
		public function get todo():uint {
			return m_todo;
		}
		/**
		 * How many activity elements were done correctly. Elements
		 * that were not touched by counted as done nor as errors. 
		 */
		public function get done():uint {
			return m_done;
		}
		/**
		 * How many activity elements were done incorrectly. Elements
		 * that were not touched by counted as done nor as errors. 
		 */
		public function get errors():uint {
			return m_errors+m_errorsUncounted;
		}
		
		/**
		 * Number of errors that will be substracted from the result. Not all
		 * errors may take part in result counting - it is configured externally.  
		 */
		public function get errorsInResult():uint {
			return m_errors;
		}
		/**
		 * The percentage result counted from todo/done/errors. The result is rounded
		 * downwards (floor) to whole numbers.
		 * @return percentage result or Number.POSITIVE_INFINITY if there is nothing
		 * todo in this result
		 */
		public function get result():Number {
			return m_todo==0 ? Number.POSITIVE_INFINITY : Math.floor( 100*Math.max(0,m_done-m_errors)/m_todo );
		}
		/**
		 * Should the percentage result be calculated with errors substracted
		 * from done.
		 * @see #append()
		 */
		public function get includeErrorsInResult():Boolean {
			return m_includeErrors;
		}
		
		/**
		 * Add to this result some other result. Used by activity containers
		 * to gather results from activities to one result.
		 * <br><br>
		 * <br>Warning to includeErrorsInResult: errors, that were on some
		 * level cut from inclusion (by turning on some child result 
		 * includeErrors in result off) will never return to play. If this
		 * result ignores errors in the result, all children errors will be excluded
		 * from the result.
		 */
		public function append( ir:Result ):void {
			if(ir == null)
				return;
				
			m_evaluationPending = m_evaluationPending || ir.evaluationPending;
			m_done += ir.done;
			m_todo += ir.todo;
			// max no. of errors included in calculating result must not be greater then no. of "done" excercise elements
			var errCounted:int = (ir.errorsInResult > ir.done) ? ir.done : ir.errorsInResult;
			// depending on "includeErrorsInResult" style value, errors are appended to appropriate property 
			if ( m_includeErrors ) {
				m_errors += errCounted;
				m_errorsUncounted += ir.errors-errCounted;
			} else
				m_errorsUncounted += ir.errors;
		}
		
		/**
		 * Checks if this result is equal to some other result.
		 * @param r other result
		 * @return true if results are equals
		 */
		public function equals( ir:Result ):Boolean {
			if(ir == null)
				return false;
			return	m_evaluationPending==ir.evaluationPending &&
					m_todo==ir.todo && 
					m_done==ir.done && 
					errors==ir.errors &&
					errorsInResult==ir.errorsInResult;
		}
		
		protected function init(_todo:uint, _done:uint, _errors:uint, _includeErrorsInResult:Boolean, _evalPending:Boolean ):void {
			m_todo = _todo;
			m_done = _done;
			m_includeErrors = _includeErrorsInResult;
			m_evaluationPending = _evalPending;
			
			// errors go to the apropriate property depending _inlcude error flag
			if ( _includeErrorsInResult )
				m_errors = _errors;
			else
				m_errorsUncounted = _errors;
		}
		
		public function saveState( saveStateContext:IStateSaveContext ):void{
			var dataOut:IDataOutput = saveStateContext.dataOutput;
			
			dataOut.writeUnsignedInt(m_todo);
			dataOut.writeUnsignedInt(m_done);
			dataOut.writeUnsignedInt(m_errors);
			dataOut.writeUnsignedInt(m_errorsUncounted);
			
			dataOut.writeBoolean(m_evaluationPending);
			dataOut.writeBoolean(m_includeErrors);		
		}
		
		public function loadState( loadStateContext:IStateLoadContext ):void{
			var dataIn:IDataInput = loadStateContext.dataInput;
			
			m_todo = dataIn.readUnsignedInt();
			m_done = dataIn.readUnsignedInt();
			m_errors = dataIn.readUnsignedInt();
			m_errorsUncounted = dataIn.readUnsignedInt();
			
			m_evaluationPending = dataIn.readBoolean();
			m_includeErrors = dataIn.readBoolean();
		}
	}
}